package com.stock.stockana.requests;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.StringRequest;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.StockCenter;
import com.stock.stockana.constants.NetWorkConstants;

import java.util.HashMap;
import java.util.Map;

/**
 *---------------------------------------------------
 * Description: StringRequest的抽象封装
 * Author: ljgsonx
 * Belong to: com.stock.stockana.requests
 * Time: 2016/1/28 17:19
 *---------------------------------------------------
 */
public abstract class BaseStringRequest<T extends BaseStringRequest> extends StringRequest{


    private Map<String, String> mHeaderMap = new HashMap<>();

    public BaseStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        String accessToken = StockCenter.getUserDB().getAccessToken();
        if(!StringUtil.isBlank(accessToken)){
            mHeaderMap.put(NetWorkConstants.HEADER_AUTHORIZATION, accessToken);
        }
        return mHeaderMap;
    }

    public T addHeader(String key,String value) {
        if(!StringUtil.hasBlank(key, value)){
            mHeaderMap.put(key, value);
        }
        return (T)this;
    }

    @Override
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        return super.setRetryPolicy(new DefaultRetryPolicy(
                NetWorkConstants.TIMEOUT,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }

}
